#include "assert_macros.h"
#include "qsfmlwidget.h"

/*!
 * \file      qsfmlwidget.cpp
 * \brief     QSFMLWidget implementation.
 * \author    SFML Staff
 * \author    Duriez Franck
 * \version   0.3
 */

// Platform-specific headers
#ifdef Q_WS_X11
    #include <Qt/qx11info_x11.h>
    #include <X11/Xlib.h>
#endif

////////////////////////////////////////////////////////////
/// Construct the QSFMLWidget
////////////////////////////////////////////////////////////
QSFMLWidget::QSFMLWidget(QWidget* parent, const QPoint&,
                         const QSize&, unsigned int frameTime) : /*{{{*/
        QWidget(parent),
        m_initialized(false)
{
    // Setup some states to allow direct rendering into the widget
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_OpaquePaintEvent);
    setAttribute(Qt::WA_NoSystemBackground);

    // Set strong focus to enable keyboard events to be received
    setFocusPolicy(Qt::StrongFocus);

    // Setup the widget geometry
    //move(position);
    //resize(size);

    // Setup the timer
    m_timer.setInterval(frameTime);
} /*}}}*/


////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
QSFMLWidget::~QSFMLWidget() /*{{{*/
{
    // Nothing to do...
} /*}}}*/

float* QSFMLWidget::getElapsedTime() /*{{{*/
{
    return &m_elapsed_time;
} /*}}}*/

////////////////////////////////////////////////////////////
/// Return the paint engine used by the widget to draw itself
////////////////////////////////////////////////////////////
QPaintEngine* QSFMLWidget::paintEngine() const /*{{{*/
{
    return NULL;
} /*}}}*/


////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
////////////////////////////////////////////////////////////
void QSFMLWidget::showEvent(QShowEvent* event) /*{{{*/
{
    if (event->type() == QEvent::Show)
    {
        // Under X11, we need to flush the commands sent to the server to ensure that
        // SFML will get an updated view of the windows
        #ifdef Q_WS_X11
            XFlush(QX11Info::display());
        #endif

        // Create the SFML window with the widget handle
        Create(winId());
        //Create(sf::VideoMode(800, 600, 32), "Tetris", sf::Style::Resize);
        // Let the derived class do its specific stuff
        initialize();

        // Setup the timer to trigger a refresh at specified framerate
        connect(&m_timer, SIGNAL(timeout()), this, SLOT(repaint()));
        m_timer.start();
    }
} /*}}}*/

////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
////////////////////////////////////////////////////////////
void QSFMLWidget::paintEvent(QPaintEvent*) /*{{{*/
{
    // mesure the time elapsed since the last call
    m_elapsed_time = m_clock.GetElapsedTime();
    m_clock.Reset();

    // Let the derived class do its specific stuff
    update();

    // Display on screen
    Display();
} /*}}}*/

void QSFMLWidget::resizeEvent(QResizeEvent*) /*{{{*/
{
    /// \bug Display problem from here
    refresh();
} /*}}}*/

void QSFMLWidget::refresh() /*{{{*/
{
    Create(winId());
} /*}}}*/
